<template>
	<view class="main" v-if="open">

		<view style="display: flex;align-items: center;  margin-bottom: 50rpx;">
			<u--input placeholder="请输入" border="surround" v-model="searchValue"
				@input="$u.debounce(emitChange, 300)"></u--input>
			<view style="margin-left: 20rpx;">
				<u-icon name="close" @click="open = false" size="24px"></u-icon>
			</view>

		</view>
		<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="lower">
			<view id="demo1" class="scroll-view-item uni-bg-red">
				<view class="item" @click="comfrim(chooseDC)" v-if="chooseDC[keyName]">
					<view class="item_name">{{ chooseDC[keyName] }}</view>
				</view>
				<view class="item" v-for="item in list" :key="item.id" @click="comfrim(item)">
					<view class="item_name">{{ item[keyName] }}</view>
					<view class="item_address">{{ item[keyInfo] || '' }}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default: () => []
			},
			keyName: {
				type: String,
				default: 'name'
			},
			keyInfo: {
				type: String,
				default: 'address'
			},
			title: {
				type: String,
				default: '所属品牌'
			},
			keyId: {
				type: [String, Number],
				default: 'id'
			},
			//跳转页面
			page: {
				type: Number
			}
		},
		data() {
			return {
				searchValue: '',
				open: false,
				pageNumber: 1,
				chooseDC: {}
			}
		},
		watch: {
			list(val) {
				this.changeInput()
				if (!val.length > 0 && this.page) return this.topage()
			}
		},
		methods: {
			emitChange(val) {
				this.changeInput()
				this.$emit('inChange', this.searchValue, 1);
			},
			changeInput(){
				if(this.page){
					return;
				}
				let ind = this.list.findIndex(item => item[this.keyName] == this.searchValue);
				console.log("chooseDc", this.list, ind)
				if (ind > -1) {
					this.chooseDC = {};
				} else {
					this.chooseDC[this.keyId] = this.searchValue;
					this.chooseDC[this.keyName] = this.searchValue;
				}
				this.$forceUpdate();
			},
			comfrim(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否确认选择',
					success: function(res) {
						if (res.confirm) {
							that.$emit('name', item[that.keyName]);
							that.$emit('input', item[that.keyId]);
							that.open = false;
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			lower() {
				this.pageNumber += 1;
				this.$emit('inChange', this.searchValue, this.pageNumber);
			},
			topage() {
				let that = this
				uni.showModal({
					title: '提示',
					content: '暂无,是否创建',
					success: function(res) {
						if (res.confirm) {
							let url = '';
							switch (that.page) {
								case 1:
									url = '/pages/releases/brandCorrelation/brandCorrelation';
									break;
								case 2:
									url = '/pages/releases/addPoint/addPoint';
									break;
							}
							uni.navigateTo({
								url
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style>
	.main {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #ffffff;
		padding: 40rpx;
		z-index: 999999999999999999999999999999999999999999999999999999999999;
	}

	.item {
		margin: 25rpx 0;
	}

	.item .item_name {
		font-size: 30rpx;
		font-weight: 600;
	}

	.item .item_address {
		font-size: 20rpx;
		margin-top: 10rpx;
		color: #999;
	}

	.scroll-Y {
		height: 90%;
		margin-top: 40rpx;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.closeIcon {
		display: flex;
		justify-content: flex-end;
		height: 40px;
		text-align: right;
	}
</style>